package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.ItemSet;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class MyCollectActivity extends Base2Activity {
    private RecyclerView rcv_list;
    private TextView empty_view;

    private List<ItemSet> collectItemList = new ArrayList<>();// 收藏的项目列表
    private CollectListAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_collect;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        rcv_list = findViewById(R.id.rcv_list);
        empty_view = findViewById(R.id.empty_view);

        adapter = new CollectListAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        setHeadTitle("我的收藏");

        setListeners();
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideProgress();
        reqCollectList();
        showProgress();
    }

    private void setListeners() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                ItemSet itemSet = collectItemList.get(position);
                if (TextUtils.equals("1", itemSet.getType())) {// 0 项目 1 套餐
                    bundle.putString("type", "2");
                } else {
                    bundle.putString("type", "1");
                }
                bundle.putString("id", itemSet.getId());// 这个是处理过的id,本来的id是收藏id
                startActivity(ItemSetDetailActivity.class, bundle);
            }
        });
    }

    private Disposable reqCollectList() {
        return APIRetrofit.getDefault().reqMyCollectList(Global.HEADER, 1, 999999)
                .compose(RxUtil.<BaseResult2<List<ItemSet>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<ItemSet>>>() {
                    @Override
                    public void accept(BaseResult2<List<ItemSet>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {
                            collectItemList = listBaseResult2.getData();
                            if (!isDataEmpty(collectItemList)) {
                                // 由于收藏的项目和套餐id为收藏id,pid为项目或套餐id,所以数据需要处理一下
                                for (int i = 0; i < collectItemList.size(); i++) {
                                    collectItemList.get(i).setId(collectItemList.get(i).getPid());
                                }
                            }
                            adapter.setDataList(collectItemList);
                            if (isDataEmpty(collectItemList)) {
                                empty_view.setVisibility(View.VISIBLE);
                            } else {
                                empty_view.setVisibility(View.GONE);
                            }
                        } else {
                            ToastUtils.showShort(listBaseResult2.getMsg());
                        }
                        if (isDataEmpty(collectItemList)) {
                            ToastUtils.showShort("暂无数据");
                        } else {

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        empty_view.setVisibility(View.VISIBLE);
                    }
                });
    }

    private class CollectListAdapter extends ListBaseAdapter<ItemSet> {

        public CollectListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_my_collect_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_price = holder.getView(R.id.tv_price);
            TextView tv_day = holder.getView(R.id.tv_day);
            TextView tv_sig = holder.getView(R.id.tv_sig);

            ItemSet set = mDataList.get(position);
            tv_name.setText(set.getName());
            tv_price.setText("¥" + set.getPrice());
            tv_day.setText(set.getDay() + "个工作日");
            tv_sig.setText(set.getSignificance());
        }
    }
}